/**
 * 能力评估表
 * */
import React from 'react';
import { Button, Popconfirm, Table, message, Spin } from 'antd';
import { history } from 'umi'
import { useRequest, defaultFormatResult } from '@/hooks';
import authHelper from '@/utils/auth/helper';
import { DividerContainer } from '@/molecules';
import { Icon } from '../../../components';
import { getEvaluationList, deleteEvaluation } from '../../service';

const { checkCode } = authHelper;

const InterviewEvaluation = () => {
    // 获取面试评估表
    const { data: evaluationList, loading: evaluationLoading, run: fetchEvaluationList } = useRequest(
        getEvaluationList,
        {
            manual: true,
            initialData: [],
            ...defaultFormatResult
        }
    );

    // 删除面试评估表
    const { run: delEvaluation } = useRequest(deleteEvaluation, {
        manual: true,
        onSuccess: res => {
            const { code, msg } = res;
            if (code === 10000) {
                fetchEvaluationList();
                message.success('操作成功');
                return;
            }
            message.error(msg);
        }
    });

    const handleDelete = id => {
        if (id) {
            delEvaluation(id);
        }
    };

    React.useEffect(() => {
        fetchEvaluationList();
    }, []);

    const columns = [
        {
            title: '名称',
            dataIndex: 'formName',
            ellipsis: true
        },
        {
            title: '操作',
            dataIndex: 'operation',
            width: 100,
            fixed: 'right',
            render: (text, record) => (
                <DividerContainer>
                    {checkCode('office:recruitment:setting:interview:evaluation:update') && (
                        <Icon
                            title="编辑"
                            type="listfunction_edit_icon_default"
                            onClick={() => {
                                history.push({
                                    pathname: '/office/personnelManagement/recruitment/setting/evaluation/edit',
                                    query: {
                                        id: record.id
                                    }
                                });
                            }}
                        />
                    )}
                    {checkCode('office:recruitment:setting:interview:evaluation:delete') && (
                        <Popconfirm
                            title="确定删除该评估表吗？"
                            okText="确定"
                            cancelText="取消"
                            onConfirm={() => handleDelete(record.id)}
                        >
                            <Icon title="删除" type="listfunction_delete_icon_default" onClick={() => {}} />
                        </Popconfirm>
                    )}
                </DividerContainer>
            )
        }
    ];

    return (
        <Spin spinning={evaluationLoading}>
            <Table rowKey="id" columns={columns} dataSource={evaluationList} pagination={false} />
            {checkCode('office:recruitment:setting:interview:evaluation:create') && (
                <Button
                    type="dashed"
                    icon="plus"
                    block
                    style={{ marginTop: 12 }}
                    onClick={() => {
                        history.push({
                            pathname: '/office/personnelManagement/recruitment/setting/evaluation/add'
                        });
                    }}
                >
                    添加
                </Button>
            )}
        </Spin>
    );
};

export default InterviewEvaluation;
